package com.lzp.wechat.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.lzp.wechat.common.constants.RedisConstants;
import com.lzp.wechat.common.utils.RedisCacheUtils;
import com.lzp.wechat.entity.WeiXinBaseResponse;
import com.lzp.wechat.entity.WeiXinParameter;
import com.lzp.wechat.entity.templateMessage.IndustryRequest;
import com.lzp.wechat.entity.templateMessage.IndustryResponse;
import com.lzp.wechat.entity.templateMessage.TemplateMessageRequest;
import com.lzp.wechat.entity.templateMessage.TemplateMessageResponse;
import com.lzp.wechat.service.TemplateMessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class TemplateMessageServiceImpl implements TemplateMessageService {

    @Autowired
    private WeiXinParameter weiXinParameter;

    @Autowired
    private RedisCacheUtils redisCacheUtils;

    @Override
    public WeiXinBaseResponse setIndustry(IndustryRequest industryRequest) {
        String access_token = redisCacheUtils.get(RedisConstants.ACCESS_TOKEN).toString();
        String url = weiXinParameter.getSetIndustryUrl();
        url = url.replace("ACCESS_TOKEN", access_token);
        String result = HttpUtil.post(url, JSON.toJSONString(industryRequest));
        log.info("调用腾讯设置所属行业响应：{}", result);
        return JSON.parseObject(result, WeiXinBaseResponse.class);
    }

    @Override
    public IndustryResponse getIndustry() {
        String access_token = redisCacheUtils.get(RedisConstants.ACCESS_TOKEN).toString();
        String url = weiXinParameter.getGetIndustryUrl();
        url = url.replace("ACCESS_TOKEN", access_token);
        String result = HttpUtil.get(url);
        log.info("调用腾讯获取所属行业响应：{}", result);
        return JSON.parseObject(result, IndustryResponse.class);
    }

    @Override
    public TemplateMessageResponse sendTemplateMessage(TemplateMessageRequest request) {
        String access_token = redisCacheUtils.get(RedisConstants.ACCESS_TOKEN).toString();
        String url = weiXinParameter.getSendTemplateMessageUrl();
        url = url.replace("ACCESS_TOKEN", access_token);
        String result = HttpUtil.post(url, JSON.toJSONString(request));
        log.info("调用腾讯发送模板消息响应：{}", result);
        return JSON.parseObject(result, TemplateMessageResponse.class);
    }

}
